﻿
using Xacml.Core.Interfaces;
using Xacml.Core.Runtime;
using System.Text.RegularExpressions;

namespace Xacml.Core.Runtime.Functions
{
  public class RegexpStringMatch : FunctionBase
  {
    public override string Id
    {
      get
      {
        return "urn:oasis:names:tc:xacml:1.0:function:regexp-string-match";
      }
    }

    public override IDataType Returns
    {
      get
      {
        return (IDataType) DataTypeDescriptor.Boolean;
      }
    }

    public override IDataType[] Arguments
    {
      get
      {
        return new IDataType[2]
        {
          (IDataType) DataTypeDescriptor.String,
          (IDataType) DataTypeDescriptor.String
        };
      }
    }

    public override EvaluationValue Evaluate(EvaluationContext context, params IFunctionParameter[] args)
    {
      if (new Regex(this.GetStringArgument(args, 0)).Match(this.GetStringArgument(args, 1)).Success)
        return EvaluationValue.True;
      return EvaluationValue.False;
    }
  }
}
